package com.qyy.www_chatroom.controller;

import com.qyy.www_chatroom.mapper.MessageMapper;
import com.qyy.www_chatroom.mapper.MessageSessionMapper;
import com.qyy.www_chatroom.model.Friend;
import com.qyy.www_chatroom.model.MessageSession;
import com.qyy.www_chatroom.model.MessageSessionUser;
import com.qyy.www_chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/session")
public class MessageSessionController {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageSessionMapper messageSessionMapper;

    @RequestMapping("/sessionList")
    public Object getMessageList(HttpServletRequest request){
        List<MessageSession> messageSessionList = new ArrayList<>();

        //1.获取当前用户userId，从Spring的Session中获取
        HttpSession session = request.getSession(false);
        if(session == null){
            System.out.println("getMessageList Session为空！");
            return messageSessionList;
        }

        User user = (User)session.getAttribute("user");
        if(user == null){
            System.out.println("getMessageList : user 为空");
        }
        //2.根据userId查询数据库，查出来会话有哪些。
        List<Integer> sessionIdList = messageSessionMapper.getSessionIdByUserId(user.getUserId());
        System.out.println("获取到的sessionIdList："+sessionIdList);
        for(int sessionId : sessionIdList){
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);

            //3.遍历会话Id，查询出每个会话里涉及到的好友有谁
            List<Friend> friends = messageSessionMapper.getFriendsBySessionId(sessionId, user.getUserId());
            messageSession.setFriends(friends);

            //4.遍历会话 iD，查询每个会话最后一条消息
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage == null){
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(lastMessage);

            }
            messageSessionList.add(messageSession);
        }
        //最终目标构造出一个MessageSession对象数组。
        return  messageSessionList;


    }



    @Transactional
    @RequestMapping("/session")
    public Object addMessageSession(int toUserId, @SessionAttribute("user")User user){
        HashMap<String,Integer> resp = new HashMap<>();
        //进行数据库插入操作
        //1.先给message—_Session表里插入记录,使用这个参数的目的主要是为了获取到会话的主键SessionId
        //里面的friends和lastMessage属性这里用不上
        MessageSession messageSession = new MessageSession();
        messageSessionMapper.addMessageSession(messageSession);

        //2.给message_session_user表里插入记录
        MessageSessionUser messageSessionUser = new MessageSessionUser();
        messageSessionUser.setSessionId((messageSession.getSessionId()));
        messageSessionUser.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(messageSessionUser);

        //3.给message_session_user表中插入记录
        MessageSessionUser messageSessionUser2 = new MessageSessionUser();
        messageSessionUser2.setSessionId(messageSession.getSessionId());
        messageSessionUser2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(messageSessionUser2);

        System.out.println("[addMessageSession] 新增会话成功！sessionId =" + messageSession.getSessionId()
        +"userId" +user.getUserId()+"userId2" + toUserId);

        resp.put("sessionId", messageSession.getSessionId());
        return resp;
    }
}
